package GUI;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.Font;

@SuppressWarnings("serial")
/**
 * View the user card of a specific user
 * @author Benny
 *
 */
public class ViewUserCardLibrarianGUI extends JPanel {
	public JTextField textUserID;
	public JTextField textName;
	public JTextField textID;
	public JTextField textEmail;
	public JTextField textPhone;
	public JTextField textUserStatus;
	public MainFrame mf;
	public DefaultTableModel searchTableModel1;
    public JTable table1;
    public JScrollPane scrollPane1;
	/**
	 * initialize all the text fields
	 */
	public void init(){
		textUserID.setText("");
		textName.setText("");
		textID.setText("");
		textEmail.setText("");
		textPhone.setText("");
		textUserStatus.setText(""); 
		
		for(int i=0;i<mf.librarianMain.viewUser.searchTableModel1.getRowCount();i++)
        	mf.librarianMain.viewUser.searchTableModel1.removeRow(i);
        if(mf.librarianMain.viewUser.searchTableModel1.getRowCount()==1)
        	mf.librarianMain.viewUser.searchTableModel1.removeRow(0);
	}
	
	/**
	 * check for userID validity
	 * @return user's ID validity status
	 */
	public int checkID(){
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{3})$";
        CharSequence inputStr = textUserID.getText();
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches();

		if(matchFound) return 1;
		else return 0;
    }

	/**
	 * Create the panel.
	 * @param M Instance of the main Frame of the program 
	 */
	public ViewUserCardLibrarianGUI(MainFrame M) {
		setPreferredSize(new Dimension(600, 520));
		setMaximumSize(new Dimension(600, 520));
		setMinimumSize(new Dimension(600, 520));
		setLayout(null);
		mf=M;
		JLabel lblViewUserCard = new JLabel("View User Card");
		lblViewUserCard.setFont(new Font("Segoe UI", Font.BOLD, 16));
		lblViewUserCard.setBounds(231, 12, 143, 14);
		add(lblViewUserCard);
		
		JLabel lblUserid = new JLabel("UserID:");
		lblUserid.setBounds(123, 53, 151, 14);
		add(lblUserid);
		
		textUserID = new JTextField();
		textUserID.setBounds(295, 50, 126, 20);
		add(textUserID);
		textUserID.setColumns(10);
		
		searchTableModel1= new DefaultTableModel();
        searchTableModel1.addColumn("Name");
        searchTableModel1.addColumn("CopyID");
        searchTableModel1.addColumn("Return Date");
        searchTableModel1.addColumn("Status");

		scrollPane1 = new JScrollPane();
		scrollPane1.setBounds(25, 281, 545, 200);
		add(scrollPane1);
		
		table1= new JTable(searchTableModel1){
		        
		        public boolean isCellEditable(int rowIndex, int colIndex) {
		                  return false; //Disallow the editing of any cell
		                  }
		                  };
		
        
        scrollPane1.setViewportView(table1);
        table1.setRowSelectionAllowed(true);
        table1.setVisible(true);
		
		JButton btnView = new JButton("View");
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int userID = 0;
				if(checkID()==1)
				{
					ArrayList<Object> send = new ArrayList<>();
					userID=Integer.decode(textUserID.getText());
					send.add("2");
					send.add("ViewUserCard2");
					send.add(userID);
					mf.client.accept(send);
				}
				else JOptionPane.showMessageDialog(null, "UserID must be a 9-digit number", "Program", JOptionPane.ERROR_MESSAGE);
			
				ArrayList<Object> send = new ArrayList<>();
				send.add("2");
				send.add("ViewBorrowsForUser");
				send.add(userID);
				mf.client.accept(send);
				
				init();				
			}
		});
		btnView.setBounds(481, 49, 89, 23);
		add(btnView);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(123, 102, 151, 14);
		add(lblName);
		
		JLabel lblId = new JLabel("Id:");
		lblId.setBounds(123, 127, 151, 14);
		add(lblId);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(123, 152, 151, 14);
		add(lblEmail);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(123, 177, 151, 14);
		add(lblPhone);
		
		JLabel lblUserStatus = new JLabel("User Status:");
		lblUserStatus.setBounds(123, 202, 151, 14);
		add(lblUserStatus);
		
		textName = new JTextField();
		textName.setEditable(false);
		textName.setBounds(295, 99, 126, 20);
		add(textName);
		textName.setColumns(10);
		
		textID = new JTextField();
		textID.setEditable(false);
		textID.setBounds(295, 124, 126, 20);
		add(textID);
		textID.setColumns(10);
		
		textEmail = new JTextField();
		textEmail.setEditable(false);
		textEmail.setBounds(295, 149, 126, 20);
		add(textEmail);
		textEmail.setColumns(10);
		
		textPhone = new JTextField();
		textPhone.setEditable(false);
		textPhone.setBounds(295, 174, 126, 20);
		add(textPhone);
		textPhone.setColumns(10);
		
		textUserStatus = new JTextField();
		textUserStatus.setEditable(false);
		textUserStatus.setBounds(295, 199, 126, 20);
		add(textUserStatus);
		textUserStatus.setColumns(10);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(481, 83, 89, 23);
		add(btnClear);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		
		JLabel lblActiveBorrows = new JLabel("Active Borrows:");
		lblActiveBorrows.setBounds(25, 245, 98, 14);
		add(lblActiveBorrows);
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(683, 575);
		add(picLabel);
	}

}
